package com.codebo.homiematch.controller;

import com.codebo.homiematch.common.BaseResponse;
import com.codebo.homiematch.common.ResultUtils;
import com.codebo.homiematch.model.dto.LoginDTO;
import com.codebo.homiematch.model.dto.RegisterDTO;
import com.codebo.homiematch.model.vo.UserVO;
import com.codebo.homiematch.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController
{
    @Resource
    private UserService userService;

    @PostMapping("/login")
    public BaseResponse<UserVO> login(@RequestBody LoginDTO loginDTO, HttpServletRequest httpServletRequest)
    {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        UserVO userVO = userService.login(username, password, httpServletRequest);
        return ResultUtils.success(userVO);
    }

    @PostMapping("/register")
    public BaseResponse<Long> register(@RequestBody RegisterDTO registerDTO)
    {
        String username = registerDTO.getUsername();
        String password = registerDTO.getPassword();
        String checkPassword = registerDTO.getCheckPassword();
        String nickname = registerDTO.getNickname();
        List<String> tags = registerDTO.getTags();
        int longitude = registerDTO.getLongitude();
        int latitude = registerDTO.getLatitude();
        Long id = userService.register(username, password, checkPassword, tags, nickname, longitude, latitude);
        return ResultUtils.success(id);
    }
}
